<template>
  <div class="my-container">
    <div class="header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>字典管理</el-breadcrumb-item>
        <el-breadcrumb-item v-for="item in dictRoute">{{item.name}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="form">
      <el-form :inline="true">
        <el-form-item>
          <el-button type="primary" round @click="edit(true)">添加</el-button>
        </el-form-item>
        <el-form-item label="编码">
          <el-input v-model="page.cond.code" placeholder="按编码查询" clearable></el-input>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="page.cond.name" placeholder="按名称查询" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="list(true)">查询</el-button>
          <el-button type="info" round @click="goParent" v-if="dictRoute.length > 0">返回上级</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="content">
      <el-table
        :data="result.records"
        border
        empty-text="暂无数据"
        height="100%">
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column prop="code" label="编码" min-width="150"></el-table-column>
        <el-table-column prop="name" label="名称" min-width="150"></el-table-column>
        <el-table-column prop="seq" label="序号" min-width="50"></el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="150">
          <template #default="scope">
            <el-button @click="edit(null, scope.row)" type="text" link size="small">编辑</el-button>
            <el-button @click="remove(scope.row)" type="text" link size="small">删除</el-button>
            <el-button @click="goChild(scope.row)" type="text" link size="small">子级</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="footer">
      <el-pagination
        background
        layout="prev, pager, next"
        @current-change="changePage"
        :page-size="result.size"
        :total="result.total">
      </el-pagination>
    </div>
    <!-- 模态框 -->
    <el-dialog
      title="编辑"
      ref="form-dialog"
      v-model="visible.edit"
      :close-on-click-modal="false"
      width="600px">
      <div>
        <el-form ref="form" :model="info" label-width="80px" :rules="rules">
          <el-form-item label="编码" prop="code">
            <el-input v-model="info.code" placeholder="请输入编码"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="info.name" placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item label="附加值1" prop="value1">
            <el-input v-model="info.value1" placeholder="附加值1"></el-input>
          </el-form-item>
          <el-form-item label="附加值2" prop="value2">
            <el-input v-model="info.value2" placeholder="附加值2"></el-input>
          </el-form-item>
          <el-form-item label="附加值3" prop="value3">
            <el-input v-model="info.value3" placeholder="附加值3"></el-input>
          </el-form-item>
          <el-form-item label="序号" prop="seq">
            <el-input-number controls-position="right" :precision="2" :step="1" v-model="info.seq"></el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
          <span class="dialog-footer">
            <el-button @click="visible.edit = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Index",
  data() {
    return {
      visible: {
        edit: false
      },
      page: {
        current: 1,
        size: 10,
        total: 0,
        orders: [
          { column: "seq", asc: true }
        ],
        cond: {
          code: null,
          name: null,
          parentId: null
        },
      },
      result: {},
      info: {
        id: null,
        code: null,
        name: null,
        parentId: null,
        value1: null,
        value2: null,
        value3: null,
        seq: 1.0
      },
      rules: {
        code: [
          { required: true, message: "必填" }
        ],
        name: [
          { required: true, message: "必填" }
        ],
        seq: [
          { required: true, message: "必填" }
        ]
      },
      dictRoute: [], // 字典路径
      currentParent: {} // 当前父字典项
    };
  },
  mounted() {
    this.list();
  },
  methods: {
    // 分页查询
    list(first) {
      if (first) {
        this.page.current = 1;
      }
      this.page.cond.parentId = this.currentParent.id || null;
      this.$http.post("/admin/sys/dict/list", this.page, res => {
        this.result = res.data;
      }, false, true, false);
    },
    // 编辑数据,add为true表示新增
    edit(add, row) {
      this.visible.edit = true;
      this.$nextTick(() => {
        this.$utils.resetObj(this.info, null);
        if (add) {
          this.$refs["form"].resetFields();
        } else {
          this.$utils.copyObj(row, this.info);
          this.info.password = null;
        }
      });
    },
    // 保存数据
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.info.parentId = this.currentParent.id || null;
          this.$http.post("/admin/sys/dict/save", this.info, () => {
            this.visible.edit = false;
            this.list();
          }, true, true, true, this.$refs["form-dialog"].dialogRef);
        }
      });
    },
    remove(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http.post("/admin/sys/dict/remove", row, () => {
          this.list();
        }, true, true, true);
      });
    },
    // 当前页切换
    changePage(current) {
      this.page.current = current;
      this.list();
    },
    // 跳转到子级
    goChild(row) {
      this.currentParent = row;
      this.dictRoute.push(row);
      this.list(true);
    },
    // 跳转到父级
    goParent() {
      // 移出最后一个父元素
      this.dictRoute.splice(this.dictRoute.length - 1, 1);
      if (this.dictRoute.length > 0) {
        // 将最后一个元素赋值为当前父字典
        this.currentParent = this.dictRoute[this.dictRoute.length - 1];
      } else {
        this.currentParent = {};
      }
      this.list(true);
    }
  }
};
</script>

<style lang="scss" scoped>
.my-container {
  padding: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  text-align: left;
  overflow: hidden;

  .header {
    padding: 20px;
    border-bottom: 1px solid #eeeeee;
    text-align: left;
  }

  .form {
    padding: 20px 20px 0 20px;
    font-size: 14px;
    text-align: left;
  }

  .content {
    flex: 1;
    height: 0;
  }

  .footer {
    padding: 5px;
  }
}
</style>